package com.jbm.jbmsupplier.presenters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.weather.myframe.PopupSelectImage;
import com.example.weather.smoothImage.ImagePagerActivity;
import com.jbm.jbmsupplier.modes.AccmPlishInterface;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.views.AccmPlishViews;
import com.jbm.onehome.common.OHInterface;
import com.jbm.onehome.iface.IJOrder;
import com.jbm.onehome.iface.Media;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 家帮忙 on 2017/7/10.
 */

public class AccmPlishActivitys implements AccmPlishInterface{
    private Context context;
    private AccmPlishViews accmPlishViews;
    private boolean Isboo = true;
    private final int SUTMITAC = 0001;
    private final int UPMEDIA = 0002;
    ArrayList<String> tpain;
    DialogLoading dialogLoading;
    public AccmPlishActivitys(Context context, AccmPlishViews accmPlishViews) {
        this.context = context;
        this.accmPlishViews = accmPlishViews;
    }
public void setInit(){
    getInit();
               }
    @Override
    public void getInit() {//初始化数据
        tpain = new ArrayList<>();
    }
    @Override
    public void getPaizhao(int tag) {//拍照
        if (tag==1){
        if (Isboo) {
            Intent intent = new Intent(context, PopupSelectImage.class);
            accmPlishViews.setShowTupian(intent);//启动选择图片
        } else {
            Intent intent = new Intent(context, ImagePagerActivity.class);
            // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
            intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, tpain);
            intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, 0);
            context.startActivity(intent);
        }}else if (tag==2){
            tpain.clear();
            Isboo = true;
        }
    }

    @Override
    public void getTuPianFile(File file) {//获取图片的路径
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 4;
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        try {
            tpain.add(Uri.fromFile(file)+"");
            accmPlishViews.setUpdataTuPian(bitmap);//更新选择的图片
            Isboo = false;
        } catch (Exception e) {
            Log.e("main", Log.getStackTraceString(e));
        }
    }

    @Override
    public void getTopMedia(File file,int orid) {//上传图片
        dialogLoading=new DialogLoading(context,"上传中...",true);
        dialogLoading.show();
        NetIntent netIntent=new NetIntent(handler);
        Media media = new Media();
        media.mtype = "pic";
        media.filepath = file.getPath();
        media.filename =file.getName();
        media.m_memo="确认单";
        netIntent.setUpMedia(orid,media,UPMEDIA);
    }

    @Override
    public void getSemdMessage() {//发送报修内容
        dialogLoading=new DialogLoading(context,"上传中...",true);
        dialogLoading.show();
        Thread thread=new Thread(runnable);
        thread.start();

    }

    @Override
    public void getSemdMessages(final int orid, final String fuwnr, final String cailnr, final String fuwje, final String cailje, String zje, final boolean pay) {
        dialogLoading=new DialogLoading(context,"上传中...",true);
        dialogLoading.show();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        final String data = df.format(new Date());// new Date()为获取当前系统时间
        Observable.create(new Observable.OnSubscribe<IJOrder>() {
            @Override
            public void call(Subscriber<? super IJOrder> subscriber) {
                subscriber.onNext(OHInterface.getG_server().SubmitJob(orid,fuwje,fuwnr,cailje,cailnr,data,pay));
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<IJOrder>() {
            @Override
            public void call(IJOrder ijOrder) {
                if (ijOrder.m_code==200){
                    accmPlishViews.setSucced(dialogLoading,1);
                }else {
                    accmPlishViews.setError(ijOrder.m_info,dialogLoading);
                }

            }
        });
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SUTMITAC:
                    IJOrder iorder=(IJOrder)msg.obj;
                    if (iorder.m_code==200){
                      accmPlishViews.setSucced(dialogLoading,1);
                    }else  if (iorder.m_code==500){
                        accmPlishViews.setError(iorder.m_info,dialogLoading);
                    }
                    break;
                case UPMEDIA:
                    Media media=(Media)msg.obj;
                    if (media.m_code==200){
                    if (media.mid>0){
                        accmPlishViews.setSucced(dialogLoading,2);
                    }}else if (media.m_code==500){
                        accmPlishViews.setError(media.m_info,dialogLoading);
                    }
                    break;
            }
        }
    };
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
//            Calendar c = Calendar.getInstance();
//            c.add(Calendar.MONTH, Integer.parseInt(accmPlishViews.getGainBxq()+ "") + 1);
//            int year = c.get(Calendar.YEAR);
//            int month = c.get(Calendar.MONTH);
//            int day = c.get(Calendar.DAY_OF_MONTH);
//            String date = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day);
//
            NetIntent netintent = new NetIntent(handler);
            netintent.setAccomPlis(accmPlishViews.getGainOrid(),accmPlishViews.getGainBxq(),accmPlishViews.getGainWxnr(),accmPlishViews.getGainWxjg(),SUTMITAC);

        }
    };

}
